<?
// Establish domain prefix
$domainname=explode('.',$domaindata['domain']);
$domainprefix=$domainname[0];
if ($cfg['prepend_domain_to_username']) {
	// Establish part name
	$partname = str_replace($domainprefix."-","",$personaldata['username']);
} else {
	$partname = $personaldata['username'];
}

// If data is set (i.e the submit button has been hit)
if (isset($data)) {
	if (is_array($data)) {
		// remove spaces and slashes.
		while (list($key, $val) = each($data))
			$data[$key] = trim(stripslashes($val));

		// HARDCODED $data['uid'], 'id'
		$data['uid'] = $personaldata['username'];
		$data['id'] = $personaldata['id'];
		//Check restrictions to change user name
		if ($userdata['level'] > $cfg['un_change_level']) {
			$data['username'] = $personaldata['username'];
		}
		if ($cfg['prepend_domain_to_username']) {
			// HARDCODED $data['username'] to include domainprefix
			$data['username']=$domainprefix."-".$data['username'];
		}
		//We cant't change access level ourselves if restrict_access_level_change enabled
		if (($cfg['restrict_access_level_change'] == true) AND ($userdata['username'] == $personaldata['username'])) {
			$data['level'] = $personaldata['level'];
		}

		call_hook('web:personal:userset:submit', $data);

		$error = user("update",$data);
		if ($error)
			echo "<br>&nbsp;&nbsp;&bull; ".implode("<br>\n&nbsp;&nbsp;&bull; ",$error)."<br>\n";
		else {
			// if username / skin / lang changed, display a message & reload.
			if ($data['username'] != $personaldata['username']
				OR $data['skin'] != $personaldata['skin']
				OR $data['lang'] != $personaldata['lang']) {
				echo "<br><div align='center'><b>".$T['user updated']." <a href=\"./?cp=$cp&url=$url&number=$number&user=".$data['username']."&confirm=".time()."\" target=\"_top\">".$T['Reload']."</a></b></div><br>
<script language='JavaScript1.3'>
<!--
load_url('./?cp=$cp&url=$url&number=$number&user=".$data['username']."&confirm=".time()."')
// -->
</script>";
				// Send user cookies
				if ($cfg['cookiesec']) {
					send_cookie("skin",$data['skin'],time()+5000000,"",$cfg['sysname'], $cfg['ssl']?1:0);
					send_cookie("lang",$data['lang'],time()+5000000,"",$cfg['sysname'], $cfg['ssl']?1:0);
				} else {
					send_cookie("skin",$data['skin'],time()+5000000,"",$_SERVER['HTTP_HOST']);
					send_cookie("lang",$data['lang'],time()+5000000,"",$_SERVER['HTTP_HOST']);
				}
			} else {
				echo "<br><div align='center'><b>".$T['user updated']."</b></div><br>\n";
			}

			// update $personaldata
			$personaldata = fetchdata("*","user",array($data['username'], $number));
		}
	}
}
// If we're back from a reload (i.e confirm is set), display message.
if ($confirm)
	echo "<br><div align='center'><b>".$T['user updated']."</b></div><br>\n";
?>
<div align="center">
<form action="<?=$current_url;?>" method="post" name="webcp" onSubmit="submitonce(this);">
<table border="0" cellspacing="0" cellpadding="0"><tr><td>
<table border="0" cellspacing="0" cellpadding="0" class="tblbg">
	<tr>
		<td>
		<table border="0" cellspacing="1">
			<tr>
				<td colspan="2" class="tbltop">&nbsp;<?=$T['menu'][$url];?>&nbsp;</td>
			</tr>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['Full Name'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<input type="text" name="data[name]" size="30" maxlength="255" value="<?=$personaldata['name'];?>">&nbsp;</td>
			</tr>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['Username'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<?=(($cfg['prepend_domain_to_username']) ? $domainprefix."-" : "");?><? if ($userdata['level'] < $cfg['un_change_level']) { ?><input type="text" name="data[username]" size="30" maxlength="255" value="<?=$partname;?>"><? } else echo $partname; ?>&nbsp;</td>
			</tr>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['Password'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<input type="text" name="data[password]" size="15" maxlength="25" value="<? if ($userdata['level'] <= 1 AND $userdata['type'] != 'demo' AND $cfg['ssl']) echo $personaldata['password'];?>">&nbsp;&nbsp;<?=$T['confirm'];?> <input type="checkbox" name="data[pass_confirm]" value="1">&nbsp;</td>
			</tr>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['User Root Directory'];?>&nbsp;</b></td>
			<? // get dir listing to get possible user roots
				$i = 0;
				unset($dir_info);
				// get default values
				$tmp = str_replace($cfg['webdir']."/".$personaldata['id'],"",$personaldata['root']);
				$tmp = ereg_replace("/+","/",$tmp);
				if (!$tmp) $tmp = "/";
				$dir_info[$i++] = $tmp;
				$dir_info[$i++] = "";

				// get dir data
				$tmp_array = dir_list($cfg['webdir']."/".$personaldata['id']."/","dir");

				//make it all look nice
				do {
					$tmp = str_replace($cfg['webdir']."/".$personaldata['id'],"",current($tmp_array));
					$tmp = ereg_replace("/+","/",$tmp);
					if ($tmp) $dir_info[$i++] = $tmp;
				} while (next($tmp_array));

			?>

				<td class="row2">
				&nbsp;<?
				if ($userdata['level'] < 4) {
					echo '<select name="data[root]">';
					$i = 0;
					while (isset($dir_info[$i])) {
						echo '<option value="'.$dir_info[$i].'"> ';
						$tmp = $dir_info[$i];
						if (strlen($tmp) > 25)
							$tmp = substr($tmp, 0, 5)."[..]".substr($tmp, strlen($tmp)-16, 16);
						else
							$tmp =  $dir_info[$i];
						echo "$tmp</option>\n";
						++$i;
					}
					echo '</select>';
				}
				else echo $dir_info[0];
			?>&nbsp;</td>
			</tr>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['Access Level'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<select name="data[level]">

    				<? //We cant't change access level ourselves if restrict_access_level_change enabled
				    if (($cfg['restrict_access_level_change'] == true) AND ($userdata['username'] == $personaldata['username']))   {
					    echo '<option value="'.$personaldata['level'].'" selected>'.$T['userlvl'][$personaldata['level']].' </option>';
 	    			} else {
						for ($i=4; $i>=$userdata['level'];$i--) {
							echo '<option value="'.$i.'"';
							if ($personaldata['level'] == $i) echo " selected";
							echo "> ".$T['userlvl'][$i]."</option>\n";
						}
					}
					?>
                           </select>
                           &nbsp;</td>
			</tr>
			<?
			// display possible IP Restrictions
			$tmp = explode('.', get_remote_ip());
			$row[0] = "";
			$row[1] = $tmp[0].'.';
			$row[2] = $tmp[0].'.'.$tmp[1].'.';
			$row[3] = $tmp[0].'.'.$tmp[1].'.'.$tmp[2].'.';
			$row[4] = $tmp[0].'.'.$tmp[1].'.'.$tmp[2].'.'.$tmp[3];
			if ($personaldata['ip_restrict'])
				$ipr[$personaldata['ip_restrict']] = ' checked';
			?>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['IP Restriction'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<select name="data[ip_restrict]">
				<? for ($i=0; $i <=4; $i++) {
					echo "<option value='".$row[$i]."'";
					if ($personaldata['ip_restrict'] == $row[$i]) echo ' selected';
					if ($row[$i])
						echo "> ".$row[$i]."</option>\n";
					else
						echo "> ".$T['none']."</option>\n";
				} ?></select>&nbsp;</td>
			</tr>
			<? // Get domain data
			$domaindata = fetchdata("shell,hd","domain",$personaldata['id']);

				if ($domaindata['shell'] == "on") {
					$check[$personaldata['shell']] = ' checked';
					?>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['Shell Access'];?>&nbsp;</b></td>
				<td class="row2">
				<? if ($userdata['level'] < 4) {?>
				&nbsp;<input type="radio" name="data[shell]" value="on"<?=$check['on'];?>><?=$T['on'];?>
				<input type="radio" name="data[shell]" value="off"<?=$check['off'];?>><?=$T['off'];?>&nbsp;
				<? } else {
					echo $personaldata['shell'];
				}
				?>
				</td>
			</tr>
			<? } ?>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['Hard Disk Space'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<?=$personaldata['hd_used'];?> / <? if ($userdata['level'] < 4) { ?><input type="text" name="data[hd]" size="5" maxlength="255" value="<? if ($personaldata['hd'] != -1) echo intval($personaldata['hd']); ?>" id="hd" onchange="document.getElementById('hdcheck').checked = false;">&nbsp;<?=$T['mb'];?>&nbsp;<input type=checkbox id="hdcheck" name="data[hd]" value="-1" onclick="document.getElementById('hd').value = '';" <? if ($personaldata['hd'] == -1) echo "checked";?> <? if ($domaindata['hd'] != -1) { echo " disabled"; }?>><?=$T['Unlimited'];?><? } else echo (($personaldata['hd'] == -1) ? $T['Unlimited'] : intval($personaldata['hd'])." ".$T['mb']);?>&nbsp;</td>
			</tr>
			<tr>
			<? // get available languages
				if (strstr($cfg['lang'],",")) {
					$language = explode(",",$cfg['lang']);
				}
				else $language[1] = $cfg['lang'];
				?>
				<td class="row1"><b>&nbsp;<?=$T['Language'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<select name="data[lang]">
				<? do {
					echo '<option value="'.trim(current($language)).'"';
					if (current($language) == $personaldata['lang']) echo " SELECTED";
					echo '> '.$T[current($language)].'</option>';
					echo "\n";
				} while(next($language)); ?>
                           </select>&nbsp;</td>
			</tr>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['Default Page'];?>&nbsp;</b>
				<div align="right">&nbsp;<?=$T['Reset'];?><input type="checkbox" name="bookmark" value="reset">&nbsp;</div></td>
				<td class="row2">
				<? // check & parse user's default page
					if ($personaldata['favorites']) {
						$tmp = explode(":",$personaldata['favorites']);
						$fav_cp = $T['menu'][$tmp[0]];
						$fav_url = $T[$tmp[0]][$tmp[1]];
				   	}
				   	else {
				  		$fav_cp = $T['none'];
				  		$fav_url = $T['none'];
				   	}
				?>
				<table border="0" cellpadding="0" summary="">
					<tr>
						<td class="row2">&nbsp;<?=$T['Panel'];?>&nbsp;</td>
						<td class="row2">&nbsp;<?=$fav_cp;?>&nbsp;</td>
					</tr>
					<tr>
						<td class="row2">&nbsp;<?=$T['Page'];?>&nbsp;</td>
						<td class="row2">&nbsp;<?=$fav_url;?>&nbsp;</td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				<?
					// fetch reseller data to check any limitation on skin usage
					$rdata = fetchdata("skin","reseller",$personaldata['id']);
					// Check if a skin list exists, if so parse it.  Else use default skin.
					if (file_exists("skin/list")) {
						unset($skin_info);
						$i = 0;
						$j = 0;
						$tmp = file("skin/list");
						while ($tmp[$i]) {

							$tmp_info = split("[[:space:]]+",$tmp[$i]);
							if (count($tmp_info) >= 2) {
								if ($rdata['skin'] == "any" OR $rdata['skin'] == $tmp_info[0]) {

									$skin_info[$j]['dir'] = array_shift($tmp_info);
									if (count($tmp_info) > 1)
										$skin_info[$j]['name'] = implode(" ",$tmp_info);
									else
										$skin_info[$j]['name'] = $tmp_info[0];
									++$j;
								}
							}
							++$i;
						}

						// Check if we actually got anything.
						if (count($skin_info) < 1) {
							unset($skin_info);
							$skin_info[0]['dir'] = $cfg['defaultskin'];
							$skin_info[0]['name'] = $cfg['defaultskin'];
						}
					}
					else {
						$skin_info[0]['dir'] = $cfg['defaultskin'];
						$skin_info[0]['name'] = $cfg['defaultskin'];
					}
				?>
				<td class="row1"><b>&nbsp;<?=$T['Interface Look'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<select name="data[skin]">
				<? // print skin choice(s)
					$i = 0;
					while ($skin_info[$i]) {
						echo '<option value="'.$skin_info[$i]['dir'].'"';
						if ($personaldata['skin'] == $skin_info[$i]['dir']) echo " SELECTED";
						echo '> '.$skin_info[$i]['name'].'</option>';
						echo "\n";
						++$i;
					}
				?>
                           </select>&nbsp;</td>
			</tr>
<?php
$ret = array();
call_hook('web:personal:userset:table', $ret);
foreach($ret as $item) {
?>
			<tr>
				<td class="row1"><b>&nbsp;<?=$item['name'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<?=$item['value'];?>&nbsp;</td>
			</tr>
<?php
}
?>
		</table>
		</td>
	</tr>
</table>
<br><div align="right"><input type="submit" value="<?=$T['Update User'];?>"></div>
</td></tr></table>
</form></div>
